/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.com.umcs.funds;

import java.math.BigDecimal;

/**
 *
 * @author User
 */
 enum Currency
    {   PLN, 
        EUR
    }


 public class Money
 {
        public static final Currency DEFAULT_CURRENCY = Currency.EUR;
        public static final Money ZERO = new Money();

        private BigDecimal value;
        
        private Currency currency;
        
        protected Money()
        {
        }

        public Money(BigDecimal value, String currencyCode)
        {
            this.value = value;
            this.currency = determineCurrencyFromCode(currencyCode);
        }

        public Money(BigDecimal value, Currency currency)
        {
            this.value = value;
            this.currency = currency;
        }

        public Money(BigDecimal value)            
        {
            this(value, DEFAULT_CURRENCY);
        }

        public Money(double value, Currency currency)            
        {
            this(new BigDecimal(value), currency);                    
        }

        public Money(double value)            
        {
            this(value, DEFAULT_CURRENCY);
        }

        public Money MultiplyBy(double multiplier)
        {
            return MultiplyBy(new BigDecimal(multiplier));
        }

        public Money MultiplyBy(BigDecimal multiplier)
        {
            return new Money(multiplier.multiply(getValue()), getCurrency());
        }

        /*
         *  Currency is compatible if the same or either money object has zero value
         */ 
        private boolean compatibleCurrency(Money money)
        {
            return isZero(getValue()) || isZero(money.getValue()) || 
                    getCurrency().equals(money.getCurrency());
        }

        private static Boolean isZero(BigDecimal testedValue){
            return BigDecimal.ZERO.compareTo(testedValue) == 0;
        }

        private Currency determineCurrency(Money otherMoney)
        {
            return isZero(getValue()) ? otherMoney.getCurrency() : getCurrency();            
        }

        private Currency determineCurrencyFromCode(String currencyCode)
        {
            switch (currencyCode)
            {
                case "PLN": return Currency.PLN;
                           
                case "EUR": return Currency.EUR;
                           
                default:
                            return DEFAULT_CURRENCY;
            }          
        }

        public Money Add(Money money)
        {
            if (!compatibleCurrency(money)){
                throw new IllegalArgumentException("Money of diffrent Currencies");
            }
            return new Money(getValue().add(money.getValue()), determineCurrency(money));
        }

        public Money Subtract(Money money)
        {
            if (!compatibleCurrency(money)){
                throw new IllegalArgumentException("Money of diffrent Currencies");
            }
            return new Money(getValue().subtract(money.getValue()), determineCurrency(money));
        }

        public boolean GreatherThan(Money other)
        {
            if (!compatibleCurrency(other)){
                throw new IllegalArgumentException("Money of diffrent Currencies");
            }
            return getValue().compareTo(other.getValue()) > 0;
        }

        public boolean LessThan(Money other)
        {
            if (!compatibleCurrency(other)){
                throw new IllegalArgumentException("Money of diffrent Currencies");
            }
            return getValue().compareTo(other.getValue()) < 0;
        }

        public boolean LessOrEquals(Money other)
        {
            if (!compatibleCurrency(other)){
                throw new IllegalArgumentException("Money of diffrent Currencies");
            }
            return getValue().compareTo(other.getValue()) <= 0;
        }

        public  boolean equals(Object obj)
        {
            if (obj instanceof Money){
                Money money = (Money)obj;
                return compatibleCurrency(money) 
                        && getValue().compareTo(money.getValue()) == 0;
            }
            return false;
        }

    /**
     * @return the value
     */
    public BigDecimal getValue() {
        return value;
    }

    /**
     * @return the currency
     */
    public Currency getCurrency() {
        return currency;
    }
    public static void main(String[] args){
    System.out.print("Ucz się gościu ucz, bo nauka to potęgi klucz, a jak będziesz miał dużo kluczy to zostaniesz woźnym");
  }
    public static void ABCD(){
        System.out.println("djkadgdgbc cfgdghjf");
    }
    
    
 }

